<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!-- 3.给表单设置model属性传入表单数据 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        :status-icon="true"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model.trim="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model.trim="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="btn-login"
            >登录</el-button
          >
        </el-row>
      </el-form>
      <el-link class="loginLink" @click="$router.push('/reg')">去注册</el-link>
      <!-- <router-link class="loginLink" to="/reg">去注册</router-link> -->
    </div>
  </div>
</template>

<script>
export default {
  namespaced: true,
  data () {
    return {
      // 1.定义验证规则
      ruleForm: {
        password: '',
        username: ''
      },
      // 2.在rule中彷入表单校验规则
      rules: {
        // 每个表单项的规则是一个数组
        // 每个数组元素代表一个校验规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{6,12}$/,
            message: '密码长为6-12位',
            trigger: 'blur'
          }
          // min: 最少输入 3 个字符
          // max: 最多输入 5 个字符
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z]{1}[a-zA-Z0-9]{1,9}$/,
            message: '用户名是1-10位字母和数字,且不能以数字开头',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 用于清空表单值
          // this.$refs[formName].resetFields();
          const res = await this.$http.post('/api/login', this.ruleForm)
          // 将token存到vuex中，也存入本地存储
          this.$store.commit('updateToken', res.data.token)
          // if (res.data.code) alert(res.data.message)
          if (res.data.code === 0) {
            this.$router.push('/')
            return this.$message({
              type: 'success',
              message: res.data.message
            })
          }
          this.$message({
            type: 'error',
            message: res.data.message
          })
        } else {
          console.log('登录失败!!')
          // return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;
  position: relative;
  .reg-box {
    width: 400px;
    height: 270px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
  .demo-ruleForm {
    font-size: 12px;
    width: 340px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    .btn-login {
      width: 100%;
    }
  }

  .loginLink {
    font-size: 12px;
    color: #a6a9ad;
    text-decoration: none;

    position: absolute;
    bottom: 20px;
    left: 30px;
  }
}
</style>
